﻿using System;
using System.Globalization;
using System.IO;
using Avalonia.Data.Converters;
using LuckyCoding.Models;
using Material.Icons;

namespace LuckyCoding;

public class FileConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is string s)
            return Enum.Parse<MaterialIconKind>(s);

        if (value is FolderModel folder)
        {
            if (File.Exists(folder.Location))
            {
                return Path.GetExtension(folder.Location) switch
                {
                    ".cs" => MaterialIconKind.LanguageCsharp,
                    ".css" => MaterialIconKind.LanguageCss3,
                    ".c" => MaterialIconKind.LanguageC,
                    ".cpp" => MaterialIconKind.LanguageCpp,
                    ".java" => MaterialIconKind.LanguageJava,
                    ".js" => MaterialIconKind.LanguageJavascript,
                    ".html" => MaterialIconKind.LanguageHtml5,
                    ".xaml" or "axaml" or ".xml" => MaterialIconKind.LanguageXaml,
                    ".swift" => MaterialIconKind.LanguageSwift,
                    ".md" => MaterialIconKind.LanguageMarkdown,
                    ".ttf" => MaterialIconKind.Language,
                    _ => MaterialIconKind.File
                };
            }

            return MaterialIconKind.Folder;
        }
        
        if (value is CodeFileModel file)
        {
            return file.Extension switch
            {
                ".cs" => MaterialIconKind.LanguageCsharp,
                ".css" => MaterialIconKind.LanguageCss3,
                ".c" => MaterialIconKind.LanguageC,
                ".cpp" => MaterialIconKind.LanguageCpp,
                ".java" => MaterialIconKind.LanguageJava,
                ".js" => MaterialIconKind.LanguageJavascript,
                ".html" => MaterialIconKind.LanguageHtml5,
                ".xaml" or "axaml" or ".xml" => MaterialIconKind.LanguageXaml,
                ".swift" => MaterialIconKind.LanguageSwift,
                ".md" => MaterialIconKind.LanguageMarkdown,
                ".ttf" => MaterialIconKind.Language,
                _ => MaterialIconKind.File
            };
        }

        if (value is SettingFileModel) return MaterialIconKind.Settings;

        if (value is SettingFileModel) return MaterialIconKind.Internet;
        
        return value;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return value;
    }
}